<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm"  :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="展会全称"  prop="exhName">
            <el-input v-model="dataForm.exhName" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="展会简称" prop="exhSName">
            <el-input v-model="dataForm.exhSName" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="英文名称">
            <el-input v-model="dataForm.exhEName" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属项目" prop="exhProject">
            <el-select v-model="dataForm.exhProject" placeholder="请选择">
              <el-option v-for="role in exhProjectList" :key="role.id" :label="role.name" :value="role.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间">
            <el-date-picker v-model="dataForm.exhBTime"  placeholder="开始日期"  type="date" value-format="YYYY-MM-DD HH:mm:ss" ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间">
            <el-date-picker v-model="dataForm.exhETime"  placeholder="结束日期" type="date" value-format="YYYY-MM-DD HH:mm:ss"  ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属展馆">
            <el-select v-model="dataForm.exhEhcId" placeholder="请选择" @change="getPostList">
              <el-option v-for="role in roleList" :key="role.id" :label="role.name" :value="role.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属展厅">
            <el-select v-model="dataForm.exhShowRooms" placeholder="请选择">
              <el-option v-for="role in exhibitionList" :key="role.id" :label="role.name" :value="role.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="举办城市">
            <el-select v-model="dataForm.exhCity" placeholder="请选择">
              <el-option v-for="role in exhCityList" :key="role.name" :label="role.name" :value="role.name"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="展会类别">
            <ren-select v-model="dataForm.exhType" placeholder="请选择" dict-type="exh_type"></ren-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属行业" prop="exhTrade">
            <el-input v-model="dataForm.exhTrade" placeholder="请选择"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预登记展会关联">
            <el-select v-model="dataForm.pregExhId" placeholder="请选择">
              <el-option v-for="role in exhibition" :key="role.id" :label="role.template" :value="role.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item style="margin-top: 50px;">
            <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
            <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  exhName: "",//展会全称
  exhSName: "",//展会简称
  exhEName: "",//英文名称
  exhTrade: "",//所属行业
  exhBTime: "",//开始日期
  exhETime: "",//结束日期
  exhCity: "",//展会城市
  exhType: "",//展会类别
  exhProject: "",//所属项目
  exhShowRooms: "",//所属展厅
  exhEhcId: "",//所属展馆
  pregExhId: "",//预登记展会
});

//做校验
const rules = ref({
  exhName:[{ required: true, message: t("validate.required"), trigger: "blur" }],
  exhSName:[{ required: true, message: t("validate.required"), trigger: "blur" }],
  exhProject:[{ required: true, message: t("validate.required"), trigger: "blur" }],

});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  //所属项目
  getexhProjectList()
  //举办城市
  getexhCityList()
  //预登记展会关联
  getExhibition()

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
      getInfo(id);
    }

};


// 获取信息
const getInfo = (id: number) => {
  baseService.get("/crm/crmexhibition/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

/**
 *项目查询
 */
const exhProjectList = ref<any[]>([]);
const getexhProjectList = () => {
  return baseService.get("/crm/crmexhiproject/queryList").then((res) => {
    exhProjectList.value = res.data;
  });
};

//展馆列表
const roleList = ref<any[]>([]);
const getRoleList = () => {
  return baseService.get("/crm/crmexhibitioncenter/getList").then((res) => {
    console.log(res);
    roleList.value = res.data;
  });
};

//展馆展厅
const exhibitionList = ref<any[]>([]);
const getPostList = () => {
  console.log(dataForm.exhEhcId);
  return baseService.get("/crm/crmshowroom/getListByEhcId/" + dataForm.exhEhcId).then((res) => {
    exhibitionList.value = res.data;
  });
};

/**
 *举办城市
 */
const exhCityList = ref<any[]>([]);
const getexhCityList = () => {
  return baseService.get("/sys/region/getListByLevel/2").then((res) => {
    exhCityList.value = res.data;
  });
};

/**
 *预登记展会关联
 */
 const exhibition = ref<any[]>([]);
const getExhibition = () => {
  return baseService.get("/crm/noctemplate/list").then((res) => {
    //后台展会按照id倒序排列
    exhibition .value = res.data;
    /*return exhibition.value.sort((a, b) => {
      if (a.template < b.template) return 1;
      if (a.template > b.template) return -1;
      return 0;
    });*/
  });
};


// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/crm/crmexhibition",dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
<style lang="scss" scoped>

.el-form-item{
  width: 85%;
}
</style>
